﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace testApp
{
    public partial class DepartmentForm : Form
    {
        DataManager dm = new DataManager();
        int dbResultID;
        Department Dept { get; set; }

        public static bool Execute(Department dept)
        {
            using(var fm = new DepartmentForm())
            {
                fm.Dept = dept;
                return fm.ShowDialog() == DialogResult.OK;
            }
        }

        public DepartmentForm(int depID, string actionType)
        {
            InitializeComponent();

            Department dep = new Department();
            if (dep.DepartmentID > 0)
            {
                this.lblTopDepartment.Visible = true;
                this.cbxParentDepartment.Visible = true;
                this.cbxParentDepartment.SelectedValue = dep.ParentDepartmentID;
            }
            else
            {
                if (actionType == "edit")
                {
                    this.lblTopDepartment.Visible = false;
                    this.cbxParentDepartment.Visible = false;
                    this.cbxParentDepartment.DataSource = null;
                }
            }
            switch (actionType)
            {
                case "add":
                    dep.CreateDate = DateTime.Now;
                    dep.ParentDepartmentID = depID;
                    this.Text = "Add New Department";
                    this.cbxParentDepartment.DataSource = dm.GetDepartmentsAll();
                    this.cbxParentDepartment.DisplayMember = "DepartmentName";
                    this.cbxParentDepartment.ValueMember = "DepartmentID";
                    this.cbxParentDepartment.SelectedValue = dep.ParentDepartmentID;
                    break;
                case "edit":
                    this.Text = "Edit Department";
                    dep = dm.GetDepartmentByID(depID);
                    if (depID != 0)
                    {
                        this.cbxParentDepartment.DataSource = dm.GetDepartmentsAllExceptID(depID);
                        this.cbxParentDepartment.DisplayMember = "DepartmentName";
                        this.cbxParentDepartment.ValueMember = "DepartmentID";
                        this.cbxParentDepartment.SelectedValue = dep.ParentDepartmentID;
                    }
                    break;
            }
            
           
            this.tbxDepartmentID.Text = dep.DepartmentID.ToString();
            this.tbxDepartmentName.Text = dep.DepartmentName;
            this.dateTimePicker.Value = dep.CreateDate;
        }

        private string ValidateForm()
        {
            string result = "";
            if (this.tbxDepartmentName.Text == "")
            {
                result += "Department name is empty! ";
                if (this.dateTimePicker.Value == null)
                {
                    result += "Create date is empty!";
                }
            }
            else
            {
                result += "success";
            }
            
            return result;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string result = ValidateForm();
            if (result == "success")
            {
                Department newDep = new Department();
                newDep.DepartmentID = int.Parse(this.tbxDepartmentID.Text.ToString());
                newDep.DepartmentName = this.tbxDepartmentName.Text;
                newDep.CreateDate = this.dateTimePicker.Value;
                if (this.cbxParentDepartment.DataSource != null)
                {
                    newDep.ParentDepartmentID = int.Parse(this.cbxParentDepartment.SelectedValue.ToString());
                }  
                if (this.Text == "Add New Department")
                {
                   dbResultID = dm.AddNewDepartment(newDep);
                }
                else
                {
                   dbResultID = dm.EditDepartmentByID(newDep);
                }
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                var res = MessageBox.Show(result);
            }

            
            
        }




    }
}
